package com.thd.springboottest.camunda.controller;

import com.thd.springboottest.camunda.service.WorkFlowService;
import org.camunda.bpm.engine.ProcessEngine;
import org.camunda.bpm.engine.runtime.ProcessInstance;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * com.thd.springboottest.camunda.controller.CamundaController
 *
 * @author: wanglei62
 * @DATE: 2022/5/26 17:01
 **/
@Controller
@RequestMapping("/camunda/gateway")
public class Camunda04gateway {
    @Autowired
    private ProcessEngine processEngine;
    @Autowired
    private WorkFlowService workFlowService;
    /**
     * 部署
     */
    // url : http://127.0.0.1:8899/thd/camunda/gateway/deploy
    @RequestMapping(value="/deploy",method = RequestMethod.GET)
    @ResponseBody
    public ResponseEntity<String> deploy(){
        String deployId = this.workFlowService.deploy("processes/04gateway.bpmn");
        System.out.println("deployId:" + deployId);
        return ResponseEntity.ok("SUCCESS");
    }

    /**
     * 开启流程
     */
    // url : http://127.0.0.1:8899/thd/camunda/gateway/startProcess/[businessKey:gateway001]
    @RequestMapping(value="/startProcess/{businessKey}",method = RequestMethod.GET)
    @ResponseBody
    public ResponseEntity<String> startProcess(@PathVariable String businessKey){
        ProcessInstance pi = processEngine.getRuntimeService().startProcessInstanceByKey("gateway",businessKey);
       System.out.println("流程实例ID:" + pi.getProcessInstanceId());
        return ResponseEntity.ok("SUCCESS");
    }

    /*
    设置流程变量
    http://127.0.0.1:8899/thd/camunda/addVariableForProcessInstance/[processInstanceId]

    {
        "a": 1,
        "x": 1,
        "y": 1,
        "z":0
    }
     */

    /*
    完成代办http://127.0.0.1:8899/thd/camunda/completeTask/f462fb36-dffd-11ec-908d-e86a64fc0114
     */

    /*
    查询待办
    http://127.0.0.1:8899/thd/camunda/queryTaskByBusinessKey/gateway001
    测试并行网管，有两个待办 taskB1和 taskB2
     */

    /*
    完成两个代办http://127.0.0.1:8899/thd/camunda/completeTask/f462fb36-dffd-11ec-908d-e86a64fc0114
     */
    /*
    查询待办
    http://127.0.0.1:8899/thd/camunda/queryTaskByBusinessKey/gateway001
    两个待办完成后,有1个待办 TASKC1
     */

    /*
    完成代办http://127.0.0.1:8899/thd/camunda/completeTask/f462fb36-dffd-11ec-908d-e86a64fc0114
     */

    /*
    查询待办
    http://127.0.0.1:8899/thd/camunda/queryTaskByBusinessKey/gateway001
    测试排他网关，有1个待办 TASKD1
     */

    /*
    完成代办http://127.0.0.1:8899/thd/camunda/completeTask/f462fb36-dffd-11ec-908d-e86a64fc0114
     */

 /*
    查询待办
    http://127.0.0.1:8899/thd/camunda/queryTaskByBusinessKey/gateway001
    有1个待办 TASKE1
     */
 /*
    完成代办http://127.0.0.1:8899/thd/camunda/completeTask/9f31f4fa-e01c-11ec-9d2b-e86a64fc0114
     */

 /*
    查询待办
    http://127.0.0.1:8899/thd/camunda/queryTaskByBusinessKey/gateway001
    测试包含网关，有2个待办 TASKF1和TASKF2
     */

 /*
    完成两个代办http://127.0.0.1:8899/thd/camunda/completeTask/9f31f4fa-e01c-11ec-9d2b-e86a64fc0114
     */

  /*
    查询待办
    http://127.0.0.1:8899/thd/camunda/queryTaskByBusinessKey/gateway001
    有1个待办 TASKG1
     */

   /*
    完成代办http://127.0.0.1:8899/thd/camunda/completeTask/1a407d7e-e01d-11ec-9d2b-e86a64fc0114
     */

   /*
    查询待办
    http://127.0.0.1:8899/thd/camunda/queryTaskByBusinessKey/gateway001
    已经没有待办了
     */

   /*
   查看流程是否结束 http://127.0.0.1:8899/thd/camunda/isEndedByBussinessKey/gateway001
   返回true,流程已关闭
    */
}
